import {post,get} from '@/utils/request.js'
export default {
	state:{
		// 登录状态
		loginStatus:false,
		// token
		token:null,
		// 用户信息
		userInfo:{},
		commission: {},
		integral: {}
	},
	mutations:{
		// 初始化登录状态
		initUser(state, data){
			state.userInfo = data.user_info
			uni.setStorageSync('userInfo',JSON.stringify(data.user_info))
		},
		// 修改用户资料时更新userInfo
		updataUser(state, data) {
			state.userInfo.avatar = data.avatar
			state.userInfo.nickName = data.nickName
		},
		// 登录
		login(state,data){
			state.loginStatus = true
			state.token = data.token
			state.userInfo = data.user_info
			// 持久化存储
			uni.setStorageSync('token',JSON.stringify(data.token))
			uni.setStorageSync('userInfo',JSON.stringify(data.user_info))
		},
		// 退出登录
		logout(state){
			state.userInfo = {}
			state.loginStatus = false
			state.token = null
			uni.removeStorageSync('userInfo')
		},
		initCommission(state, data) {
			state.commission = data
		},
		initIntegral(state, data) {
			state.integral = data
		}
	},
	actions: {
		updateUserInfo({state,commit}){
			console.log('asdfasd')
			get('/api/user/info').then(res => {
				console.log(res)
				commit('initUser',res.data)
			})
		},
	}
}